<script setup lang="ts">
import { useRouter } from 'vue-router'
import { reactive } from 'vue'
import { formatFileSize } from '@renderer/utils/index'
import { uniqBy, cloneDeep } from 'lodash'
import { DeleteOutlined } from '@ant-design/icons-vue'

interface dataProps {
  fileList: any[]
  loading: boolean
}

const router = useRouter()

const data = reactive<dataProps>({
  fileList: [],
  loading: false
})
// const win = ref()

function back() {
  router.back()
}

const chooseFiles = async () => {
  const res = await window.api.chooseFiles({
    filters: [
      { name: 'Pdf文件', extensions: ['pdf'] },
    ]
  })
  const { files } = res
  // 去重
  const resData = uniqBy([...data.fileList, ...files], 'fileName')
  console.log('resData', resData)
  data.fileList = resData
}
/** 删除 */
const handleDelete = (id) => {
  const list = cloneDeep(data.fileList)
  const newData = list.filter((item) => item.fileId !== id)
  console.log(id, newData)
  data.fileList = newData
}

/** 转换 */
const convert = async () => {
  const paths: any[] = []
  data.fileList.forEach((item) => {
    item.status = 'converting'
    paths.push({
      filePath: item.filePath,
      fileId: item.fileId,
      fileName: item.fileName
    })
  })
  data.loading = true
  const res = await window.api.compress(paths)
  data.loading = false
  console.warn('res', res)
  data.fileList.forEach((item) => {
    res.forEach((ele) => {
      if (ele.fileId === item.fileId && ele.success) {
        item.status = 'success'
      }
    })
  })
}

const tableColumns = [
  {
    name: '文件',
    dataIndex: 'name',
    key: 'name'
  },
  {
    title: '大小',
    dataIndex: 'size',
    key: 'size'
  },
  {
    title: '操作',
    key: 'action',
    width: '120px'
  }
]
</script>
<template>
  <a-page-header class="page-header" title="pdf压缩" @back="back">
    <template #extra>
      <a-button type="primary" :disabled="data.loading" @click="chooseFiles">选择文件</a-button>
      <a-button type="primary" danger :loading="data.loading" @click="convert">确定</a-button>
    </template>
  </a-page-header>
  <div class="container">
    <a-spin :spinning="data.loading">
      <a-table
        :columns="tableColumns"
        :data-source="data.fileList"
        :pagination="false"
        class="custom-table"
      >
        <template #headerCell="{ column }">
          <template v-if="column.key === 'name'">
            <span> 文件 </span>
          </template>
        </template>

        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'name'">
            <a-typography-title :level="5">{{ record.fileName }}</a-typography-title>
            <p>{{ record.filePath }}</p>
          </template>
          <template v-else-if="column.key === 'size'">
            <a-tag v-if="record.status === 'success'" :bordered="false" color="success"
              >成功</a-tag
            >
            <a-tag v-if="record.status === 'converting'" :bordered="false" color="processing"
              >请稍后...</a-tag
            >
            {{ formatFileSize(record.size) }}
          </template>
          <template v-else-if="column.key === 'action'">
            <delete-outlined
              class="opt-icon opt-icon-danger"
              @click="() => handleDelete(record.fileId)"
            />
          </template>
        </template>
        <!-- <template #title>
          <p>
            <a-typography-text type="warning"
              >从pdf中提取纯文本、图片、表格等资源，将存放在以该pdf名字命名的目录下。</a-typography-text
            >
          </p>
        </template> -->
      </a-table>
    </a-spin>
  </div>
</template>

<style lang="less" scoped>
.container {
  padding: 24px 0;
  .custom-table {
    /deep/.ant-table-title {
      padding: 0;
    }
  }
}
</style>
